<?php
class Our_View_Helper_Head extends Zend_View_Helper_Abstract {
	
	public $fullBaseUrl = null;
	
	public $jsPluginDir = '/externals/plugins';
	
	public function __construct($options) {
		$fullBaseUrl = isset($options['fullBaseUrl'])?$options['fullBaseUrl']:null;
		$this->setFullBaseUrl($fullBaseUrl);
		
		if (isset($options['jsPluginDir'])){
			$this->setJsPluginDir($options['jsPluginDir']);
		}
		
		return $this;
	}
	
	public function head() {
		return $this;
	}
	
	public function setFullBaseUrl($fullBaseUrl = null) {
		if ($fullBaseUrl === null) {
			$fullBaseUrl = APPLICATION_PATH;
		}
		$this->fullBaseUrl = $fullBaseUrl;
		return $this;
	}
	
	public function getFullBaseUrl(){
		return $this->$fullBaseUrl;
	}
	
	public function setJsPluginDir($jsPluginDir){
		$this->jsPluginDir = $jsPluginDir;
		return $this;
	}
	
	public function getJsPluginDir(){
		return $this->jsPluginDir;
	}
	
	public function meta() {
		$this->view->headMeta ()->appendHttpEquiv ( 'Content-Type', 'text/html; charset=UTF-8' );
		/*ie7 ie8*/
		$this->view->headMeta ()->appendHttpEquiv ( 'X-UA-Compatible', 'IE=EmulateIE7' );
		
	/*$locale = $this->getSession ()->getLocale ();
		$this->headMeta ()->appendHttpEquiv ( 'Content-Language', str_replace ( '_', '-', $locale ) );*/
	}
	
	public function favicon($faviconSrc = null) {
		if ($faviconSrc == null) {
			$faviconSrc = $this->view->baseUrl ( 'favicon.ico' );
		}
		if (! file_exists ( $this->fullBaseUrl . $faviconSrc )) {
			return false;
		}
		$this->view->headLink ()->headLink ( array ('rel' => 'favicon', 'href' => $faviconSrc ) );
		$this->view->headLink ()->headLink ( array ('rel' => 'icon', 'href' => $faviconSrc ) );
		$this->view->headLink ()->headLink ( array ('rel' => 'shortcut icon', 'href' => $faviconSrc ) );
	}
	
	public function jquery($jquerySrc = null) {
		if ($jquerySrc == null) {
			$jquerySrc = $this->jsPluginDir.'/js/jquery-1.6.2.min.js' ;
		}
		$this->_load(array($jquerySrc));
	}
	
	/**
	 * <code>
	 * $this->view->head()->chosen();
	 * <select data-placeholder="Choose a country..." style="width:350px;" multiple class="chzn-select"">
	 * </code>
	 */
	public function chosen() {
		//$isDebug = $this->getSession ()->getDebug () ? '' : 'min.';
		$isDebug = '';
		$files [] = $this->jsPluginDir.'/chosen/' . 'chosen.jquery' . $isDebug . '.js' ;
		$files [] = $this->jsPluginDir.'/chosen/chosen.css' ;
		$this->_load($files);
		$this->view->headScript ()->captureStart();
	    echo 'jQuery(document).ready(function() {'
	        .    'jQuery(".chzn-select").chosen();'
	        .'})'; 
		$this->view->headScript ()->captureEnd();
	}
	
	/**
	 * <code>
	 * $this->view->head()->validate('formID');
	 * <form id="formID">
	 * <input value="" class="validate[required] text-input" type="text" name="req" id="req" />
	 * </form>
	 * </code>
	 * @param validate form Id $formID
	 * @return boolean
	 */
	public function validate($formID = null){
		if ($formID == null){
			return false;
		}
		$files [] = $this->jsPluginDir.'/validationEngine/js/languages/jquery.validationEngine-en.js' ;
		$files [] = $this->jsPluginDir.'/validationEngine/js/jquery.validationEngine.js' ;
		$files [] = $this->jsPluginDir.'/validationEngine/css/validationEngine.jquery.css' ;
		$this->_load($files);
		$this->view->headScript ()->captureStart();
	    echo 'jQuery(document).ready(function() {'
	        .    'jQuery("#'.$formID.'").validationEngine();'
	        .'})'; 
		$this->view->headScript ()->captureEnd();
	}
	
	/**
	 * date select
	 * <code>
	 * $this->view->head()->date('date');
	 * <input type="text" name="date" id="date" />
	 * </code>
	 * @param String $dateID
	 * @return boolean
	 */
	public function date($dateID = null){
	    if ($dateID == null){
			return false;
		}
		$a = $this->view->headScript ();
		$files [] = $this->jsPluginDir.'/jquery-ui/js/jquery-ui-1.8.16.custom.min.js';
		$files [] = $this->jsPluginDir.'/jquery-ui/css/ui-lightness/jquery-ui-1.8.16.custom.css';
		$this->_load($files);
		$this->view->headScript ()->captureStart();
	    echo 'jQuery(document).ready(function() {'
	        .    'jQuery("#'.$dateID.'").datepicker();'
	        .'})'; 
		$this->view->headScript ()->captureEnd();
	}
	
	/**
	 * <code>
	 * $this->view->head()->popup();
	 * <a href="http://media.no-margin-for-errors.com/wp-content/themes/NMFE/images/fullscreen/2.jpg" rel="prettyPhoto" title="This is the description">
	 * <img src="http://media.no-margin-for-errors.com/wp-content/themes/NMFE/images/thumbnails/t_2.jpg" width="60" height="60" alt="This is the title" />
	 * </a>
	 * </code>
	 */
	public function popup($option = null){
		if ($option === null){
			$option = '{social_tools:false}';
		}
		$files [] = $this->jsPluginDir.'/prettyPhoto/js/jquery.prettyPhoto.js';
		$files [] = $this->jsPluginDir.'/prettyPhoto/css/prettyPhoto.css';
		$this->_load($files);
		$this->view->headScript ()->captureStart();
	    echo 'jQuery(document).ready(function() {'
	        .    'jQuery("a[rel^=\'prettyPhoto\']").prettyPhoto('.$option.');'
	        .'})'; 
		$this->view->headScript ()->captureEnd();
	}
	
	/**
	 * <code>
	 * $this->view->head()->ckeditor();
	 * <textarea id="editor1" name="editor1">&lt;p&gt;Initial value.&lt;/p&gt;</textarea>
	 * </code>
	 */
	public function ckeditor(){
		$files [] = $this->jsPluginDir.'/ckeditor/ckeditor.js';
		$this->_load($files);
		$this->view->headScript ()->captureStart();
		//echo 'var CKEDITOR_BASEPATH = \''.$this->jsPluginDir.'/ckeditor/\';';
	    echo 'jQuery(document).ready(function() {'
	        .    'CKEDITOR.replace( \'editor1\' );'
	        .'})'; 
		$this->view->headScript ()->captureEnd();
	}
	
	public function tinymce($options = null){
		if ($options === null){
			$options = 'mode : "textareas",
						theme : "advanced",
						plugins:"emotions",
					
						theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
						theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,forecolor,backcolor,emotions",
						theme_advanced_toolbar_location : "top",
						theme_advanced_toolbar_align : "left",
						theme_advanced_resizing : true,
					
						style_formats : [
							{title : \'Bold text\', inline : \'b\'},
							{title : \'Red text\', inline : \'span\', styles : {color : \'#ff0000\'}},
							{title : \'Red header\', block : \'h1\', styles : {color : \'#ff0000\'}},
						]';
		}
		$files [] = '/externals/tinymce/tiny_mce.js';
		$this->_load($files);
		$this->view->headScript ()->captureStart();
	    echo 'jQuery(document).ready(function() {'
	        .    'tinyMCE.init({'.$options.'});'
	        .'})'; 
		$this->view->headScript ()->captureEnd();		
	}
	
	public function superfish(){
		$files [] = $this->jsPluginDir.'/superfish/js/hoverIntent.js';
		$files [] = $this->jsPluginDir.'/superfish/js/superfish.js';
		$files [] = $this->jsPluginDir.'/superfish/js/supersubs.js';
		$files [] = $this->jsPluginDir.'/superfish/css/superfish.css';
		$this->view->headScript ()->captureStart();
	    echo 'jQuery(document).ready(function() {'
	        .    'jQuery("ul.sf-menu").superfish();'
	        .'})'; 
		$this->view->headScript ()->captureEnd();
	}
	
	private function _load($files) {
		if (! is_array ( $files )) {
			return false;
		}
		foreach ( $files as $file ) {
			$paths = explode ( '/', $file );
			$path = implode ( DS, $paths );
			$fileName = realpath($this->fullBaseUrl.$path);
			if (! file_exists ( $this->fullBaseUrl.$path )) {
				echo $this->fullBaseUrl.$path;
				return false;
			}
		}
		foreach ($files as $file){
			$extName = array_pop(explode('.', $file));
			switch ($extName){
				case 'js':
					$this->view->headScript ()->appendFile ( $this->view->baseUrl($file) );
					break;
				case 'css':
					$this->view->headLink()->appendStylesheet ( $this->view->baseUrl($file) );
					break;
				default:
					return false;
			}
		}
	}
	
	public function __call($method, $args) {
		if (method_exists ( $this, $method )) {
			$return = call_user_func_array ( array ($this, $method ), $args );
			return $return;
		}
	}

}